package com.exchange.repository;

import com.exchange.entity.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
    Optional<Order> findByOrderNumber(String orderNumber);
    List<Order> findByMerchantId(Long merchantId);
    List<Order> findByExchangeCodeId(Long exchangeCodeId);
    
    @Query("SELECT COUNT(o) FROM Order o WHERE o.merchantId = :merchantId")
    long countByMerchantId(@Param("merchantId") Long merchantId);
    
    @Query("SELECT COUNT(o) FROM Order o WHERE o.merchantId = :merchantId AND o.createdAt >= :startOfMonth")
    Long countByMerchantIdAndMonth(@Param("merchantId") Long merchantId, 
                                   @Param("startOfMonth") LocalDateTime startOfMonth);
    
    @Query("SELECT o FROM Order o WHERE o.merchantId = :merchantId AND o.orderStatus = :orderStatus")
    List<Order> findByMerchantIdAndOrderStatus(@Param("merchantId") Long merchantId, 
                                              @Param("orderStatus") Order.OrderStatus orderStatus);
}